<?php
namespace app\common\model;

use think\Db;
use think\Model;

class NavAnli extends Model
{
	protected $insert = ['create_time'];

    /**
     * 创建时间
     * @return bool|string
     */
    protected function setCreateTimeAttr()
    {
        return date('Y-m-d H:i:s');
    }

    //获得案例管理首页内容
    public function getAnliList($keyword,$cid,$page,$pagelimit="10")
    {
    	$map = [];
    	if (!empty($keyword)) {
            $map['title'] = ['like', "%{$keyword}%"];
        }
        if(!empty($cid)){
        	$map['nav_id'] = $cid;
        }
    	// $newslist = self::with('getNavName')->where($map)->select();
    	$anlilist = $this->where($map)->order(['create_time' => 'DESC'])->paginate($pagelimit, false, ['page' => $page]);
    	$i = 0;
    	$arr = [];
    	for(;$i<count($anlilist);$i++){
    		$nav_name = Db('nav')->where('id',$anlilist[$i]['nav_id'])->column('name'); 
    		$newslist[$i]['nav_id'] = $nav_name[0];
    	}
    	return $anlilist;
    }


     /**
     * 删除新闻的thumb和图片
     * @return 1
     */
    public function deletephoto($id)
    {
    	$fileImg = $this->where('id',$id)->value('thumb');
    	//删除缩略图
    	if($fileImg){
            if(file_exists('./'.$fileImg)){
                unlink('./'.$fileImg);
            }
        }
        //删除图片集合
        $photo = $this->where('id',$id)->value('photo');

        if($photo){
        	$arr = getphoto($photo);
        	//循环删除图集图片
        	for($i = 0;$i<count($arr);$i++){
        		if(file_exists('./'.$arr[$i])){
                	unlink('./'.$arr[$i]);
            	}
        	}           
        }
        return 1;
    }

}